package com.ict.txmsv2.services;

import com.ict.txmsv2.mapper.UserMapper;
import com.ict.txmsv2.model.ClockIn;
import com.ict.txmsv2.model.RespPageBean;
import com.ict.txmsv2.model.User;
import com.ict.txmsv2.utils.OSSutils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linzzz
 * @desc ...
 * @date 2020-12-15
 */
@Service
public class UserService implements UserDetailsService {


    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.loadUserByUsername(username);
        if (user == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        user.setRoles(userMapper.getRolesByUserId(user.getId()));
        return user;
    }

    public int personCount() {
        return userMapper.personCount();
    }

    public RespPageBean getBasic(Integer page, Integer size, String name, String dept, String major, String beginTime, String endTime) {
        RespPageBean resp = new RespPageBean();
        if(page!=null && page>0){
            page = (page - 1)*size;
            List<User> data = userMapper.getBasic(page, size, name,dept, major, beginTime, endTime);
            resp.setData(data);
            long total = userMapper.getTotal(name,dept,major,beginTime, endTime);
            resp.setTotal(total);
        }
        return resp;
    }

    public RespPageBean getDetail(Integer page, Integer size, String name, String dept, String major, String beginTime, String endTime) {
        RespPageBean resp = new RespPageBean();
        if(page!=null && page>0){
            page = (page - 1)*size;
            List<User> data = userMapper.getDetail(page, size, name,dept, major, beginTime, endTime);
            for (User user : data) {
                user.setPassword(null);
                user.setAge(null);
            }
            resp.setData(data);
            long total = userMapper.getTotal(name,dept,major,beginTime, endTime);
            resp.setTotal(total);
        }
        return resp;
    }

    public int updateDetail(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public int deleteDetail(int id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    public int deleteDetails(int[] ids) {
        int cnt = 0;
        for (int id : ids) {
            cnt+=deleteDetail(id);
        }
        return cnt;
    }


    public List<String> getAllGrade() {
        List<String> grades = userMapper.getAllGrade();
        List<String> gg = new ArrayList<>();
        for (String grade : grades) {
            String[] split = grade.split("-");
            if (!gg.contains(split[0])){
                gg.add(split[0]);
            }
        }
        return gg;
    }

    public List<User> getAllUserAndHisRoles(String name, String grade) {
        return userMapper.getAllUserAndHisRoles(name,grade);
    }

    public int updateEnable(User user) {
        Integer id = user.getId();
        return userMapper.updateEnable(id);
    }

    public List<String> getUserNames() {
        return userMapper.getUserNames();

    }



    public List<User> getUsersWithClockInByDay(String day) {
        List<User> usersWithClockInByDay = userMapper.getUsersWithClockInByDay(day);
        for (User user : usersWithClockInByDay) {
            boolean f = true;
            if(user.getClockIn()!=null){
                for (ClockIn clockIn : user.getClockIn()) {
                    if(f){
                        clockIn.setCommute("上班打卡");
                    }else {
                        clockIn.setCommute("下班打卡");
                    }
                    f = !f;
                }
            }
        }
        return usersWithClockInByDay;
    }



    public int updateAvatar(@Param("userId") Integer userId, @Param("uri") String uri) {
        User user = userMapper.selectByPrimaryKey(userId);
        String oldAvatar = user.getAvatar();
        user.setAvatar(uri);
        OSSutils.deleteAvatar(oldAvatar);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public List<User> getAllUserExcludeCurrentUser(Integer id) {
        return userMapper.getAllUserExcludeCurrentUser(id);
    }

    public boolean updatePsw(Integer userId, String oldPsw, String newPsw) {
        String getPsw = userMapper.getPswByUserId(userId);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (encoder.matches(oldPsw,getPsw)) {
            userMapper.updatePsw( encoder.encode(newPsw),userId);
            return true;
        }
        return false;
    }

    public Integer getIdByUserName(String to) {
        return userMapper.getIdByUserName(to);
    }
}
